package com.hzhc.controller.pad;

import cn.hutool.core.util.StrUtil;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.SysUser;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysUserService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: LoginController
 * @Description:
 * @version 1.0
 * @author: 功不唐捐
 * @Date: 2023-09-01
 * 
 */
@RestController
@RequestMapping("/ipad")
@RequiredArgsConstructor
public class IpadLoginController extends BaseController {


	private final SysUserService userService;


	@PostMapping("/login")
	@SysTemLog(operModul = "IPAD用户登陆", operType = "用户登陆", operDesc = "IPAD用户登陆", modular = "IPAD")
	public ResultUtil<SysUserVo> getlogin(@RequestBody SysUser user, HttpServletRequest request) {
		if (StrUtil.isBlank(user.getLoginname())) {
			return ResultUtil.build( ResultUtilCodeEnum.ACCOUNT_ERROR);
		}
		if (StrUtil.isBlank(user.getPassword())) {
			return ResultUtil.build( ResultUtilCodeEnum.PASSWORD_ERROR);
		}
		SysUserVo vo = userService.login(user.getLoginname(), user.getPassword());
		if (vo == null) {
			return ResultUtil.build(ResultUtilCodeEnum.USER_PASSWORD_ERROR);
		} else {
			String token = TokenUtil.sign(vo);
			vo.setToken(token);
			return ResultUtil.build(vo,ResultUtilCodeEnum.SUCCESS);
		}
	}

}
